package beans;

import beans.util.JsfUtil;
import controllers.PrenotazioniFacade;
import controllers.UtenteFacade;
import entities.Viaggio;
import controllers.ViaggioFacade;
import entities.Prenotazioni;
import entities.Utente;
import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import javax.inject.Inject;
import javax.faces.view.ViewScoped;
import javax.persistence.EntityManager;

@Named(value = "viaggioController")
@ViewScoped
public class ViaggioController extends AbstractController<Viaggio> implements Serializable {

    @Inject
    private ViaggioFacade ejbFacade;
    @Inject
    private PrenotazioniFacade ejbFacadePr;
    @Inject
    private UtenteFacade ejbFacadeUt;

    public ViaggioController() {
        super(Viaggio.class);
    }

    @PostConstruct
    public void init() {
        super.setFacade(ejbFacade);
    }

    @Override
    public List<Viaggio> getItems() {
        if (items == null) {
            int idUtente = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName());

            if (idUtente == 50) {
                items = this.ejbFacade.findAll();
            } else {
                items = this.ejbFacade.findFiltered(idUtente);
            }
            //items = this.ejbFacade.findAll();
        }
        return items;
    }
    
     @Override
     public void saveNew(ActionEvent event) {
        super.selected.setIdVIAGGIO(Integer.MAX_VALUE);
        super.saveNew(event);
    }

    @Override
    protected void persist(PersistAction persistAction, String successMessage) {
        if (selected != null) {
            this.setEmbeddableKeys();
            try {
                if (persistAction != PersistAction.DELETE && persistAction != PersistAction.CREATE) {
                    this.ejbFacade.edit(selected);

                } else if (persistAction == PersistAction.CREATE) {
                    //Creates the prenotazioni entity
                    Prenotazioni p = new Prenotazioni();
                    int idUtente = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName());
                    EntityManager em = ejbFacadeUt.getEntityManager();
                    Utente u = (Utente) em.createNamedQuery("Utente.findByIdUTENTE").setParameter("idUTENTE", idUtente).getSingleResult();

                    Viaggio v = selected;
                    p.setAccettato(true);
                    p.setIdUtente(u);
                    p.setIdViaggio(v);
                    p.setIdPrenotazione(Integer.MIN_VALUE);
                    this.ejbFacadePr.edit(p);
                } else {
                    this.ejbFacade.remove(selected);
                }
                JsfUtil.addSuccessMessage(successMessage);
            } catch (EJBException ex) {
                String msg = "";
                Throwable cause = JsfUtil.getRootCause(ex.getCause());
                if (cause != null) {
                    msg = cause.getLocalizedMessage();
                }
                if (msg.length() > 0) {
                    JsfUtil.addErrorMessage(msg);
                } else {
                    JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/MyBundle").getString("PersistenceErrorOccured"));
                }
            } catch (Exception ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/MyBundle").getString("PersistenceErrorOccured"));
            }
        }
    }
}
